/**
 * Created by Administrator on 2016/11/9.
 */
/**
 * Created by Administrator on 2016/11/9.
 */
(function(){
    var myCompanyBill={
        init:function(){
            this.validate();
            this.uploadImage($("#organization_code"));
            this.uploadImage($("#business_license"));
            this.uploadImage($("#tax_license"));
        },
        validate:function(){
            if($("#company_bill_form").size() > 0){
                var num = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;
                $('form#company_bill_form').submit(function(e) {
                    if(!num.test($("input[name=reg_money]").val())){
                        $("input[name=reg_money]").addClass("active");
                        layer.msg("注册资本 请输入数字", {icon:2,time:1000});
                        return false;
                    }
                    if(!num.test($("input[name=year_money]").val())){
                        $("input[name=year_money]").addClass("active");
                        layer.msg("厂房面积 请输入数字", {icon:2,time:1000});
                        return false;
                    }
                    if(!num.test($("input[name=year_money]").val())){
                        $("input[name=year_money]").addClass("active");
                        layer.msg("年营业额 请输入数字", {icon:2,time:1000});
                        return false;
                    }

                    $("#company_bill_form input.active").removeClass("active");
                    
                    var data = new FormData($("#company_bill_form")[0]); 
                    $.ajax({
                        type:"POST",
                        dataType:"json",
                        async: false,
                        url: get_userUrl()+"Ccompany/complete_update",
                        data: data,
                        processData: false,
                        contentType: false,
                        beforeSend: function() {
                            $("#company_bill_form input[type=submit]").val("正在保存...").attr("disabled", true);
                        },
                        success: function(json) {
                            if (json.status) {
                                layer.msg("操作成功,在刷新中", {icon:1,time:1000}, function(){
                                    if(json.data){window.location.href = json.data;}
                                });
                            } else {
                                $("#company_bill_form input[type=submit]").val("保存").removeAttr("disabled");
                                layer.msg(json.errmsg, {icon:2,time:1000}, function(){
                                    if(json.data){window.location.href = json.data;}
                                });
                            }
                        }
                    });

                    e.preventDefault();
                });
            }
        },
        uploadImage:function(obj){
            obj.change(function(){
                var file=this.files[0];
                if(file.size>5*1024*1024){
                    layer.msg("图片不能超过5M", {icon:2,time:1000});
                    return;
                }

                var src=window.URL.createObjectURL(file);
                var img=$(this).closest(".right").find("img");
                if(img.length>0){
                    img.attr("src",src);
                }else{
                    $(this).closest(".right").append("<img src='"+src+"'/>");
                }
            });
        }
    };
    myCompanyBill.init();
})();